package name.marz.to.go.ahg.common;

import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public final class LogFormatter extends Formatter {
    private static final String lineBreak = System.getProperty("line.separator");
    public synchronized String format(LogRecord record) {
        StringBuilder message = new StringBuilder(100);
        long millis = record.getMillis();
        String time = String.format("%tD %<tT", millis);
        message.append(time);
        message.append(' ').append(record.getLevel().toString()).append(' ');
        if (null != record.getSourceClassName()) {
			String[] s = record.getSourceClassName().split("\\.");
			message.append(s[s.length-1]);
//        	message.append(record.getSourceClassName());
        } else {
            message.append(record.getLoggerName());
        }
        message.append(' ');
        
		message.append(String.format("[%s] "
									,record.getSourceMethodName() != null ? record.getSourceMethodName() : "N/A")
					);
        
        message.append(formatMessage(record));
        message.append(lineBreak);
        
        Throwable throwable = record.getThrown();
        if (throwable != null) {
            message.append(throwable.toString());
            message.append(lineBreak);
            for (StackTraceElement trace : throwable.getStackTrace()) {
                message.append('\t');
                message.append(trace.toString());
                message.append(lineBreak);
            }
        }
        
        return message.toString();
    }
}